/**
  ****************************(C) COPYRIGHT 2019 DJI****************************
  * @file       can_receive.c/h
  * @brief      there is CAN interrupt function  to receive motor data,
  *             and CAN send function to send motor current to control motor.
  *             这里是CAN中断接收函数，接收电机数据,CAN发送函数发送电机电流控制电机.
  * @note       
  * @history
  *  Version    Date            Author          Modification
  *  V1.0.0     Dec-26-2018     RM              1. done
  *  V1.1.0     Nov-11-2019     RM              1. support hal lib
  *
  @verbatim
  ==============================================================================

  ==============================================================================
  @endverbatim
  ****************************(C) COPYRIGHT 2019 DJI****************************
  */

#ifndef CAN_RECEIVE_H
#define CAN_RECEIVE_H

#include "struct_typedef.h"

#define CHASSIS_CAN hcan1
#define GIMBAL_CAN hcan2
#define SHOOT_CAN hcan1

/* CAN send and receive ID */
typedef enum
{
  //在can_cmd_chassis函数中被分配，使用0x200作为所有底盘电机的ID，使用hcan1发送
    CAN_CHASSIS_ALL_ID = 0x200,  //底盘所有电机的ID
    CAN_3508_M1_ID = 0x201,      //1
    CAN_3508_M2_ID = 0x202,      //2
    CAN_3508_M3_ID = 0x203,      //3
    CAN_3508_M4_ID = 0x204,      //4

  //在can_cmd_gimbal函数中被分配，使用0x1ff作为所有云台电机的ID,使用hcan2发送
    CAN_GIMBAL_ALL_ID = 0x1FF,   //云台所有电机的ID
    CAN_YAW_MOTOR_ID = 0x205,    //ID为1，回传ID为0x205的电机数据
    CAN_PIT_MOTOR_ID = 0x206,    //ID为2，回传ID为0x206的电机数据
    CAN_FRIC1_ID = 0x207,        //ID为7，回传ID为0x207的电机数据
    CAN_FRIC2_ID = 0x208,        //ID为8，回传ID为0x208的电机数据

  //在can_cmd_shoot函数中被分配，使用0x1ff作为拨弹轮电机的ID，使用hcan1发送
    CAN_TRIGER_MOTOR_ID = 0x205, //ID为5，回传ID为0x205的电机数据

  //在CAN_cmd_supercap函数中被分配，使用0x210作为超级电容的发送ID
    CAN_SUPERCAP_TX_ID = 0x210,  //**>
    CAN_SUPERCAP_RX_ID = 0x211,  //<**
} can_msg_id_e;

//rm motor data
typedef struct
{
    uint16_t ecd;
    int16_t speed_rpm;
    int16_t given_current;
    uint8_t temperate;
    int16_t last_ecd;
    float totalAngle;  //累积总共角度
    int turnCount;   //转过的圈数
} motor_measure_t;
//接收到的电机数据

typedef struct
{
    uint16_t battery_voltage;
    int16_t capacitance_voltage;
    int16_t given_current;
    uint8_t capacitance_percentage;
    uint8_t power_set;
} sup_measure_t;
//接收到的超级电容数据

extern motor_measure_t motor_chassis[4];
extern motor_measure_t motor_gimbal[5];
extern sup_measure_t sup_cap[1];

/**
  * @brief          发送云台电机控制电流(0x205,0x206,0x207,0x208)
  * @param[in]      yaw: (0x205) 6020电机控制电流, 范围 [-30000,30000]
  * @param[in]      pitch: (0x206) 6020电机控制电流, 范围 [-30000,30000]
  * @param[in]      fric1: (0x207) 3508电机控制电流, 范围 [-16384,16384]
  * @param[in]      fric2: (0x208) 3508电机控制电流, 范围 [-16384,16384]
  * @retval         none
  */
extern void CAN_cmd_gimbal(int16_t yaw, int16_t pitch, int16_t fric1, int16_t fric2);


/**
  * @brief          发送ID为0x700的CAN包,它会设置3508电机进入快速设置ID
  * @param[in]      none
  * @retval         none
  */
extern void CAN_cmd_chassis_reset_ID(void);


/**
  * @brief          发送电机控制电流(0x201,0x202,0x203,0x204)
  * @param[in]      motor1: (0x201) 3508电机控制电流, 范围 [-16384,16384]
  * @param[in]      motor2: (0x202) 3508电机控制电流, 范围 [-16384,16384]
  * @param[in]      motor3: (0x203) 3508电机控制电流, 范围 [-16384,16384]
  * @param[in]      motor4: (0x204) 3508电机控制电流, 范围 [-16384,16384]
  * @retval         none
  */
extern void CAN_cmd_chassis(int16_t motor1, int16_t motor2, int16_t motor3, int16_t motor4);

/**
 * @brief Sends a command to shoot using CAN communication.
 *
 * This function sends a command to shoot to the specified motors using CAN communication.
 *
 * @param motor5 The desired speed for motor 5.
 * @param motor6 The desired speed for motor 6.
 * @param motor7 The desired speed for motor 7.
 * @param rev The desired speed for the reserve motor.
 */
/**
 * @brief 发送射击指令
 * 
 * 此函数用于发送射击指令到CAN总线。
 * 
 * @param shoot 射击机构拨弹轮速度
 */
extern void CAN_cmd_shoot(int16_t shoot , int16_t rev1, int16_t rev2, int16_t rev3);

/**
 * @brief 设置超级电容的功率
 * 
 * 此函数用于设置超级电容的功率。
 * 
 * @param power_set 设置的功率值，单位为0.01W，范围为30~120W。
 * 
 * @return 无返回值
 */
extern void CAN_cmd_supercap(int16_t power_set);

/**
  * @brief          返回yaw 6020电机数据指针
  * @param[in]      none
  * @retval         电机数据指针
  */
extern const motor_measure_t *get_yaw_gimbal_motor_measure_point(void);

/**
  * @brief          返回pitch 6020电机数据指针
  * @param[in]      none
  * @retval         电机数据指针
  */
extern const motor_measure_t *get_pitch_gimbal_motor_measure_point(void);

/**
  * @brief          返回拨弹电机 2006电机数据指针
  * @param[in]      none
  * @retval         电机数据指针
  */
extern const motor_measure_t *get_trigger_motor_measure_point(void);

/**
  * @brief          返回底盘电机 3508电机数据指针
  * @param[in]      i: 电机编号,范围[0,3]
  * @retval         电机数据指针
  */
extern const motor_measure_t *get_chassis_motor_measure_point(uint8_t i);

/**
 * @brief Get the motor measure point for the left shoot motor.
 *
 * @return A pointer to the motor_measure_t structure containing the measure point.
 */
extern const motor_measure_t *get_fric1_motor_measure_point(void);

/**
 * @brief Get the measurement data of the right shoot motor.
 *
 * This function returns a pointer to the motor_measure_t structure that contains the measurement data of the right shoot motor.
 *
 * @return const motor_measure_t* - Pointer to the motor_measure_t structure.
 */
extern const motor_measure_t *get_fric2_motor_measure_point(void);


/**
 * 获取超级电容测量点的指针
 *
 * @return 指向超级电容测量点的常量指针
 */
extern const sup_measure_t *get_supercap_measure_point(void);

#endif
